//
//  WelcomeViewController.swift
//  WeiboSwift
//
//  Created by Apple13 on 16/7/12.
//  Copyright © 2016年 yupeng. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {
    

//MARK: - 懒加载各种控件
    
    /// ①背景
    private lazy var backgroudImageView:UIImageView = {
        let imageView = UIImageView(image:UIImage(named:"ad_background"))
        return imageView
    }()
    
    //2017年04月01日
    
    /// ②头像
    private lazy var headImageView:UIImageView = {
        let imageView = UIImageView(image:UIImage(named:"avatar_default_big"))
        //指定大小
        //判断头像是否为空
        if let imageUrl = UserAccountViewModel.shareUserAccount.userAccount?.avatar_large{
            //有头像的地址
            imageView.sd_setImageWithURL(NSURL(string: imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        //设置圆角
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true

        return imageView
    }()
    
    /// ③消息Label---"欢迎回来/某某某"
    private lazy var messageLabel:UILabel = {
       let label = UILabel()
        label.font = UIFont.systemFontOfSize(15)
        label.textColor = UIColor.grayColor()
        if let name = UserAccountViewModel.shareUserAccount.userAccount?.name{
            label.text = "欢迎肥来,\(name)"
        }else{
            label.text = "欢迎肥来"
        }
        
        //设置透明度
        label.alpha = 0
        
       return label
    }()
    override func loadView() {
         view = backgroudImageView
        printLog()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
       
        setUpUI()
        printLog()
       
            
        }
        
    override func viewDidAppear(animated: Bool) {
       
        startAnimation()
         printLog()
    }

    
    
//MARK: -  添加自动布局约束
    private func setUpUI() {
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(view).offset(261)
            make.centerX.equalTo(view)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.centerX.equalTo(headImageView)
        }
        
    }
    
//MARK: - 开启动画
    private func startAnimation() {
        
        self.headImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(self.view).offset(100)

        }
        
        UIView.animateWithDuration(5, delay: 1, usingSpringWithDamping: 0.2, initialSpringVelocity: 0, options: [], animations: {
            
            //执行约束动画
             self.view.layoutIfNeeded()
            
            
            
            }) { (true) in //上面的动画完成后执行这组动画
               
                UIView.animateWithDuration(2, animations: { 
                    self.messageLabel.alpha = 1
                    }, completion: { (true) in
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVCNotification, object: nil)
                })
                
        }
       
        
        
        
    }
    
    
    
    
    
    
    
    
    
    
    
}
